<template>
  <t-dialog :header="header" :visible.sync="visible" :onClose="closeDialog"     :closeOnOverlayClick="false"
            @confirm="onSubmit" :confirmBtn="{
        content: loading?'提交中...':'确认',
        theme: 'primary',
        loading: loading,
      }" width="40%"
            top="5%">
    <t-form ref="form" :data="formData" labelWidth="120px" :rules="rules">
      <t-row style="row-gap:24px">
        <t-col :span="12">
          <t-form-item label="成果编号" name="num">
            <t-input style="width: 240px" v-model="formData.num" placeholder="请输入内容"></t-input>
          </t-form-item>
        </t-col>
        <t-col :span="12">
          <t-form-item label="成果名称" name="name">
            <t-input style="width: 240px" v-model="formData.name" placeholder="请输入内容"></t-input>
          </t-form-item>
        </t-col>
        <t-col :span="12">
          <t-form-item label="成果类型" name="type">
            <t-cascader  :keys="{value:'id'}" v-model="formData.type" :options="menuOptions" ></t-cascader>
          </t-form-item>
        </t-col>
        <t-col :span="12">
          <t-form-item label="成果说明" name="note">
            <t-textarea
              clearable :autosize="{minRows: 5}"
              v-model="formData.note"
              placeholder="请输入内容"
            />
          </t-form-item>
        </t-col>
      </t-row>
    </t-form>
  </t-dialog>
</template>

<script>
export default {
  name: "add-edit-result",
  props: ['header', 'objId', 'parentId'],
  data(){
    return {
      menuOptions: [],
      visible: true,
      loading: false,
      formData: {
        name: null,
        num: '',
        type: '',
        note: '',
      },
      rules: {
        num: [
          {required: true, message: "成果编号不能为空"}
        ],
        type: [
          {required: true, message: "成果类型不能为空"}
        ],
        name: [
          {required: true, message: "成果名称不能为空"}
        ],
      }
    }
  },
  created() {
    this.getTreeSelect();
  },
  methods:{
    onSubmit() {
      this.$refs.form.validate().then((validateResult) => {
        if (validateResult === true) {
          this.loading = true;
          if (this.objId) {
            this.$api.kjxm.review.teamUpdate(this.formData
            ).then((response) => {
              this.$message.success("修改成功");
              this.$emit("reload");
              this.$emit("update:visible", false);
              this.loading = false;
            }).catch((e) => {
              this.loading = false;
              this.$message.error(e.toString());
            });
          } else {

            this.$api.kjxm.review.teamAdd(this.formData).then((response) => {
              this.$message.success("新增成功");
              this.$emit("reload");
              this.$emit("update:visible", false);
              this.loading = false;
            }).catch((e) => {
              this.loading = false;
              this.$message.error(e.toString());
            });
          }
        }
      });
    },
    /** 查询菜单下拉树结构 */
    getTreeSelect() {
      return this.$api.system.result.treeselect().then(response => {
        this.menuOptions = response.data;
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    closeDialog() {
      this.$emit("update:visible", false);
    },
  }
}
</script>

<style scoped>

</style>
